package com.lantosec.hmrp.modules.system.controller;


import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.util.UUIDUtils;
import com.lantosec.hmrp.modules.system.entity.SysDeptPosition;
import com.lantosec.hmrp.modules.system.entity.SysPositionRole;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.modules.system.entity.SysPosition;
import com.lantosec.hmrp.modules.system.service.ISysPositionService;
import com.lantosec.hmrp.modules.system.vo.SysPositionVO;

import java.util.Arrays;
import java.util.List;

import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.RestController;
import com.lantosec.hmrp.core.mvc.BaseController;

/**
 * 岗位表 控制器
 *
 * @author AutoCode
 * @since 2020-02-12
 */
@Api(value = "岗位表控制器", tags = "岗位表控制器")
@RestController
@RequestMapping("/system/position")
@Log4j2
public class SysPositionController extends BaseController {
    @Autowired
    private ISysPositionService service;

    @GetMapping("all")
    @ApiOperation(value = "查询全部")
    public Result<List<SysPositionVO>> all() {

        return Result.ok(entity2VO(service.list()));
    }

    @GetMapping("getPositionAll")
    @ApiOperation(value = "分页查询")
    public Result<List<SysPosition>> page(PageVO<SysPosition> page) {
        List<SysPosition> sysPositionList = service.list(new QueryWrapper<SysPosition>().orderByAsc(SysPosition.ColName.position_order.toString()));
        return Result.ok(sysPositionList);
    }

    @GetMapping("get/{id}")
    @ApiOperation(value = "主键查询")
    public Result<SysPositionVO> get(@ApiParam(value = "ID", required = true) @PathVariable(name = "id") String id) {
        return Result.ok(entity2VO(service.getById(id)));
    }

    @GetMapping("getPositionByDeptId")
    @ApiOperation(value = "查询部门下的岗位(分页)")
    public Result<PageVO<SysDeptPosition>> getPositionByDeptId(@ApiParam(value = "page") PageVO<SysDeptPosition> page,
                                                               @ApiParam(value = "ID") @RequestParam(name = "deptId", required = false) String deptId) {
        PageVO<SysDeptPosition> sysDeptPosition = service.getPositionByDeptId(page,deptId);
        return Result.ok(sysDeptPosition);
    }

    @GetMapping("getPositionByDeptIdList")
    @ApiOperation(value = "查询部门下的岗位")
    public Result<List<SysDeptPosition>> getPositionByDeptIdList(@ApiParam(value = "ID") @RequestParam(name = "deptId", required = false) String deptId) {
        List<SysDeptPosition> sysDeptPosition = service.getPositionByDeptIdList(deptId);
        return Result.ok(sysDeptPosition);
    }

    @PostMapping("insertPositionInfo")
    @ApiOperation(value = "新增数据")
    public Result<String> insertPositionInfo(@RequestBody @Validated @ApiParam(value = "岗位表VO对象", required = true) SysPositionVO vo, BindingResult bindingResult) {
        validationHandler(bindingResult);
        SysPosition entity = vo.convertToEntity();
        entity.setPositionId(UUIDUtils.get());
        service.save(entity);
        return Result.ok(entity.getPositionId());
    }

    @PutMapping("updatePositionInfo")
    @ApiOperation(value = "修改数据")
    public Result<Void> updatePositionInfo(@ApiParam(value = "岗位表VO对象", required = true) @RequestBody SysPositionVO vo) {
        SysPosition entity = vo.convertToEntity();
        service.updateById(entity);
        return Result.ok();
    }

    @PutMapping("updateSysDeptPosition")
    @ApiOperation(value = "设置部门岗位的上级管理岗")
    public Result<Void> updateSysDeptPosition(@ApiParam(value = "部门岗位表对象", required = true) @RequestBody SysDeptPosition sysDeptPosition) {
        service.updateSysDeptPosition(sysDeptPosition);
        return Result.ok();
    }

    @DeleteMapping(value = "deletePositionInfo/{ids}")
    @ApiOperation("逻辑删除")
    public Result<Void> deletePositionInfo(@ApiParam(value = "ID列表,用逗号分隔", required = true) @PathVariable(name = "ids") String ids) {
        Result result = service.deletePositionInfo(ids);
        return result;
    }

    @PutMapping("updateOrder")
    @ApiOperation(value = "更新岗位管理排序")
    public Result<Void> updateOrder(@ApiParam(value = "岗位ID", required = true) @RequestParam(name = "id") String id,
                                    @ApiParam(value = "排序值", required = true) @RequestParam(name = "positionOrder") Integer positionOrder) {
        service.updateOrder(id, positionOrder);
        return Result.ok();
    }

    @GetMapping("getSysPositionRole")
    @ApiOperation(value = "查询部门岗位下的角色")
    public Result<List<SysPositionRole>> getSysPositionRole(@ApiParam(value = "部门id",required = true) @RequestParam(name = "deptId") String deptId,
                                                            @ApiParam(value = "岗位id",required = true) @RequestParam(name = "positionId") String positionId) {
        List<SysPositionRole> sysPositionRoles = service.getSysPositionRole(deptId,positionId);
        return Result.ok(sysPositionRoles);
    }

    @PutMapping("updateSysPositionRole")
    @ApiOperation(value = "更新部门岗位下的角色")
    public Result<Void> updateSysPositionRole(@ApiParam(value = "部门岗位角色",required = true) @RequestBody SysPositionRole sysPositionRole) {
        service.updateSysPositionRole(sysPositionRole);
        return Result.ok();
    }
}
